﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using YDModel;

namespace YDWin
{
   public  class cdk
    {
       //验证 授权 
        public static  bool VerificationCDK(out string VerificationStr)
        {
            bool isSuccess = true; //未过期
            VerificationStr = "已过期";
            try
            {
                using (YDDataEntities ye = new YDDataEntities())
                {
                    var sp = ye.T_System_Parameter.Where(o => o.Parameter_Name == "CDK").FirstOrDefault();
                    {
                        if (sp != null)
                        {
                            byte[] bytIn = Convert.FromBase64String(sp.Parameter_Value);
                            var newcdk = ASCIIEncoding.ASCII.GetString(bytIn);
                            newcdk = newcdk.ToLower();
                            string part1 = newcdk.Substring(41, 5);
                            string part2 = newcdk.Substring(11, 5);
                            string part3 = newcdk.Substring(25, 4);
                            string part = part1 + part2 + part3;

                            if(part.Equals("00ab66cd4245rt")) //无期限
                            {
                                isSuccess = true;
                                VerificationStr = "无期限";
                                return isSuccess; 
                            } 

                            string time = GetNetDateTime();

                            if (string.IsNullOrEmpty(time))
                            {
                                // time = DateTime.Now.ToShortDateString();
                                VerificationStr = "请保持网络畅通";
                                isSuccess = false;
                            }

                            DateTime dt=Convert.ToDateTime(part.Substring(0,4)+"-"+ part.Substring(4, 2) + "-" + part.Substring(6, 2));
                            DateTime dt2 = Convert.ToDateTime(time);

                            int days=  dt.Subtract(dt2).Days+1; 

                            if (days<1)//授权到期
                            { 
                                isSuccess = false;
                            }
                            VerificationStr = days + "天";
                        }
                        else
                        { 
                            isSuccess = false;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                VerificationStr = ex.Message.ToString();
                isSuccess = false;
            }
            

            return isSuccess;
        }

        public static string GetNetDateTime()
        {
            WebRequest request = null;
            WebResponse response = null;
            WebHeaderCollection headerCollection = null;
            string datetime = string.Empty;
            try
            {
                request = WebRequest.Create("https://www.baidu.com");
                request.Timeout = 1000;
                request.Credentials = CredentialCache.DefaultCredentials;
                response = (WebResponse)request.GetResponse();
                headerCollection = response.Headers;
                foreach (var h in headerCollection.AllKeys)
                { if (h == "Date") { datetime = headerCollection[h]; } }
                return datetime;
            }
            catch (Exception) { return datetime; }
            finally
            {
                if (request != null)
                { request.Abort(); }
                if (response != null)
                { response.Close(); }
                if (headerCollection != null)
                { headerCollection.Clear(); }
            }
        }


    }
}
